package cn.kgc.interceptor;


import cn.kgc.entity.Employee;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor {

    //卡在控制器中的业务处理方法执行之前先执行它,返回值为true代表放行请求，返回值为false代表拦截请求，
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Employee loginEmp =(Employee)request.getSession().getAttribute("loginEmp");
        if(loginEmp==null){
            response.sendRedirect(request.getContextPath()+"/");
            return false;
        }
        return true;
    }

    //卡在控制器中的业务处理方法执行之后再执行它,此时视图组件还没开始执行
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    //当视图组件执行完毕再执行它
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
